from scrapy.cmdline import execute
import pymongo
import time


client = pymongo.MongoClient('localhost', 27017)
database = client['Weather']

db_region_list = database.list_collection_names()
region_dict = {'昭通': '56586', '黔东南': '57825', '六安': '58311', '辽阳': '54347', '雅安': '56287', '北京': '54511',
               '唐山': '54534', '泸州': '57602', '黔阳': '70356', '商丘': '58005', '阿克苏': '51628', '东莞': '59289',
               '巴中': '57313', '嘉峪关': '71129', '辽源': '54260', '庆阳': '53923', '沈阳': '54342', '哈密': '52203',
               '温州': '58659', '湛江': '59658', '西宁': '52866', '随州': '57381', '金昌': '52675', '贵阳': '57816',
               '衡水': '54702', '长沙': '57687', '阿勒泰': '51076', '梅州': '59109', '吴忠': '53612', '宝鸡': '57016',
               '肇庆': '59278', '铜陵': '58429', '赣州': '57993', '石河子': '51356', '玉溪': '56875', '舟山': '58477',
               '武威': '52679', '德州': '54714', '营口': '54471', '甘南': '56080', '包头': '53446', '青岛': '54857',
               '长治': '53882', '汕尾': '59501', '柳州': '59046', '定西': '52995', '双河': '71716', '喀什': '51709',
               '大理': '56751', '焦作': '53982', '张掖': '52652', '聊城': '54806', '萍乡': '57786', '桂林': '57957',
               '嘉兴': '58452', '文昌': '59856', '凉山': '71118', '深圳': '59493', '安顺': '57806', '亳州': '58102',
               '拉萨': '55591', '淮南': '58224', '信阳': '57297', '平顶山': '71128', '呼伦贝尔': '71108',
               '徐州': '58027', '蚌埠': '58221', '玉林': '59453', '来宾': '59242', '淮安': '58141', '双鸭山': '50884',
               '本溪': '54346', '昆明': '56778', '德阳': '56198', '漯河': '57186', '中山': '59485', '商洛': '71031',
               '郑州': '57083', '怀化': '57749', '吐鲁番': '51573', '郴州': '57972', '枣庄': '58024', '安庆': '58424',
               '万宁': '59951', '七台河': '50973', '果洛': '71729', '重庆': '57516', '黑河': '50468', '榆林': '53646',
               '克拉玛依': '51243', '五指山': '60651', '巴州': '71708', '潍坊': '54843', '德宏': '71126',
               '孝感': '57482', '铁门关': '71715', '黔南': '57827', '河池': '59023', '铁岭': '54249',
               '图木舒克': '71712', '南昌': '58606', '新余': '57796', '荆门': '57377', '南京': '58238',
               '兴安盟': '60001', '赤峰': '54218', '绵阳': '56196', '崇左': '59425', '廊坊': '54515', '松原': '50949',
               '淮北': '58116', '上海': '58362', '淄博': '54830', '昌江': '59847', '三明': '58828', '伊春': '50774',
               '泉州': '59131', '济南': '54823', '昌都': '56137', '澄迈': '59843', '黄石': '58407', '日喀则': '55578',
               '沧州': '54616', '保亭': '59945', '贵港': '59249', '安康': '57245', '汉中': '57127', '周口': '57195',
               '琼海': '59855', '永州': '57865', '石家庄': '53698', '铜川': '53947', '延安': '53845', '临沂': '54938',
               '株洲': '57780', '湘潭': '57773', '佛山': '59288', '北海': '59644', '承德': '54423', '临汾': '53868',
               '忻州': '53674', '防城港': '59635', '宣城': '58433', '黔西南': '70148', '锦州': '54337', '恩施': '57447',
               '临沧': '56951', '甘孜': '56146', '合肥': '58321', '玉树': '70552', '佳木斯': '50873', '阳泉': '53782',
               '临高': '59842', '新乡': '53986', '吉安': '57799', '清远': '59280', '台州': '58651', '绥化': '50853',
               '泰安': '54827', '资阳': '56298', '西安': '57036', '黄冈': '57498', '延边': '71532', '宜宾': '56492',
               '烟台': '54765', '哈尔滨': '50953', '西双版纳': '60839', '景德镇': '58527', '岳阳': '57584',
               '阜新': '54237', '通化': '54363', '白银': '52896', '屯昌': '59854', '朔州': '53578', '攀枝花': '56666',
               '阜阳': '58203', '韶关': '59082', '菏泽': '54906', '海南': '71728', '三沙': '71444', '楚雄': '56768',
               '和田': '51828', '揭阳': '59315', '衡阳': '57872', '鹰潭': '58627', '杨凌': '71199', '高雄': '59554',
               '红河': '56975', '定安': '59851', '乐山': '56386', '宿州': '58122', '南充': '57411', '威海': '54774',
               '钦州': '59632', '常州': '58343', '昆玉市': '72108', '东方': '59838', '兰州': '52889', '开封': '57091',
               '陵水': '59954', '丽水': '58646', '仙桃': '57485', '吕梁': '71037', '宿迁': '58131', '娄底': '57763',
               '临夏': '52984', '潮州': '59312', '鹤壁': '53990', '龙岩': '58927', '鹤岗': '50775', '南通': '58259',
               '三亚': '59948', '陇南': '60472', '无锡': '58354', '马鞍山': '58336', '遵义': '57713', '杭州': '58457',
               '自贡': '56396', '莆田': '58946', '铜仁': '57741', '鞍山': '54339', '克州': '51704', '抚顺': '54353',
               '三门峡': '57051', '厦门': '59134', '珠海': '59488', '衢州': '58633', '五家渠': '71713', '镇江': '58248',
               '南宁': '59431', '天门': '57483', '固原': '53817', '曲靖': '56783', '文山': '56994', '邵阳': '57766',
               '南平': '58834', '宁德': '58846', '荆州': '57476', '海西': '71113', '山南': '55597', '泰州': '58246',
               '江门': '59473', '安阳': '53898', '湘西': '60011', '保定': '54602', '海北': '71112', '东营': '54736',
               '渭南': '57045', '张家口': '54401', '达州': '57328', '乌鲁木齐': '51463', '北屯': '71714',
               '朝阳': '54433', '酒泉': '52533', '九江': '58502', '邢台': '53798', '迪庆': '70908', '芜湖': '58334',
               '云浮': '59471', '葫芦岛': '54453', '香港': '45007', '巴彦淖尔': '60002', '惠州': '59297',
               '天水': '57006', '海东': '71727', '济源': '53978', '咸宁': '57590', '广元': '57206', '鄂州': '57496',
               '儋州': '59845', '博尔塔拉': '71710', '普洱': '70887', '锡林郭勒': '60149', '巴音郭楞': '51656',
               '河源': '59293', '张家界': '57558', '阿里': '55437', '六盘水': '56693', '广安': '57415',
               '连云港': '58044', '咸阳': '57048', '宜春': '57793', '乐东': '59940', '扬州': '58245', '通辽': '54135',
               '天津': '54527', '中卫': '53704', '长春': '54161', '神农架': '57362', '晋城': '53976', '滨州': '54734',
               '日照': '54945', '苏州': '58357', '阿拉善盟': '60356', '白沙': '59848', '十堰': '57252', '贺州': '59065',
               '秦皇岛': '54449', '白山': '54371', '丹东': '54497', '塔城': '51133', '盐城': '58151', '白城': '50936',
               '钓鱼岛': '71415', '运城': '53959', '伊犁': '71709', '澳门': '45011', '乌海': '53512', '牡丹江': '54094',
               '阿拉尔': '51730', '阿坝': '56171', '滁州': '58236', '黄山': '70931', '大庆': '50842', '太原': '53772',
               '遂宁': '57405', '盘锦': '54338', '濮阳': '54900', '百色': '59211', '池州': '58427', '黄南': '71114',
               '毕节': '57707', '海口': '59758', '抚州': '58617', '驻马店': '57290', '绍兴': '58453', '大同': '53487',
               '内江': '57504', '襄阳': '57278', '琼中': '59849', '上饶': '58637', '石嘴山': '53518',
               '可克达拉': '71717', '乌兰察布': '60150', '银川': '53614', '许昌': '57089', '洛阳': '57073',
               '怒江': '71127', '宁波': '58465', '湖州': '58450', '齐齐哈尔': '50745', '武汉': '57494', '晋中': '71115',
               '保山': '56748', '广州': '59287', '阳江': '59663', '大兴安岭': '50442', '昌吉': '51368', '丽江': '56651',
               '大连': '54662', '平凉': '53915', '吉林': '54172', '邯郸': '53892', '金华': '58549', '漳州': '59126',
               '四平': '54157', '济宁': '54915', '南阳': '57178', '益阳': '57674', '福州': '58847', '那曲': '70774',
               '鄂尔多斯': '71109', '潜江': '57475', '成都': '56294', '眉山': '56391', '呼和浩特': '53463',
               '宜昌': '57461', '常德': '57662', '林芝': '56312', '博州': '51238', '鸡西': '50978', '汕头': '59316',
               '茂名': '59659', '梧州': '59265'}

id_list = []


def crawl():
    cnt = None
    while True:
        try:
            cnt = int(input('请选择获取或更新数据城市个数:'))
        except ValueError:
            print('输入有误, 请重试')
            continue
        break
    while cnt:
        rg = input("请输入想要获取或更新数据的城市名称(市级):")
        if rg in region_dict.keys():
            cnt -= 1
            id_list.append(region_dict[rg] + '\n')
        else:
            print("城市名称有误, 请重试")
    with open('update_id.txt', 'w') as f:
        f.writelines(id_list)
    execute("scrapy crawl Weather".split())


if __name__ == '__main__':
    if len(db_region_list) == 0:
        print('当前数据库中没有城市数据, 请手动输入')
        crawl()
    else:
        print('当前已获取信息的城市:', end='')
        print(db_region_list)
        choose = int(input('请输入获取或更新数据的方式:\n1: 更新数据库中已有城市数据     2/其他: 手动输入需要获取或更新的城市数据\n>>>'))
        if choose == 1:
            for region in db_region_list:
                id_list.append(region_dict[region]+'\n')
            with open('update_id.txt', 'w') as fp:
                fp.writelines(id_list)
            execute("scrapy crawl Weather".split())
        else:
            crawl()
    print('更新完毕, 即将退出程序')
    time.sleep(5)
